/* Dump or view gcc call graph.


   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */ 

#include "gml4gtk-plugin.h"


/* Dump specific call graph into the file FNAME.  */

static void
dump_cgraph_to_file_specific (char *fname, int callee_p)
{
  g_graph *graph;

  graph = dot_plugin_common.top_graph;

  dot_plugin_common.dump (fname);

  return;
}

/* Dump call graph into the file FNAME.  */

static void
dump_cgraph_to_file (char *fname)
{
  g_graph *graph;

  graph = dot_plugin_common.top_graph;

  dot_plugin_common.dump (fname);

  return;
}

/* Local init.  */

static void
local_init (void)
{
}

/* Local finish.  */

static void
local_finish (void)
{
}

/* Public function to dump caller graph.  */

void
dot_plugin_dump_cgraph_caller (void)
{
  char *fname;

  dot_plugin_common.init ();
  local_init ();

  fname = concat (dump_base_name, ".cgraph-caller.dot", NULL);
  dump_cgraph_to_file_specific (fname, 0);
  free (fname);

  local_finish ();
  dot_plugin_common.finish ();

  return;
}

/* Public function to view caller graph.  */

void
dot_plugin_view_cgraph_caller (void)
{
  dot_plugin_common.init ();
  local_init ();

  dump_cgraph_to_file_specific (dot_plugin_common.temp_file_name, 0);
  dot_plugin_common.show (dot_plugin_common.temp_file_name);

  local_finish ();
  dot_plugin_common.finish ();

  return;
}

/* Public function to dump callee graph.  */

void
dot_plugin_dump_cgraph_callee (void)
{
  char *fname;

  dot_plugin_common.init ();
  local_init ();

  fname = concat (dump_base_name, ".cgraph-callee.dot", NULL);
  dump_cgraph_to_file_specific (fname, 1);
  free (fname);

  local_finish ();
  dot_plugin_common.finish ();

  return;
}

/* Public function to view callee graph.  */

void
dot_plugin_view_cgraph_callee (void)
{
  dot_plugin_common.init ();
  local_init ();

  dump_cgraph_to_file_specific (dot_plugin_common.temp_file_name, 1);
  dot_plugin_common.show (dot_plugin_common.temp_file_name);

  local_finish ();
  dot_plugin_common.finish ();

  return;
}

/* Public function to dump call graph.  */

void
dot_plugin_dump_cgraph (void)
{
  char *fname;

  dot_plugin_common.init ();
  local_init ();

  fname = concat (dump_base_name, (char *)".cgraph.dot", NULL);
  dump_cgraph_to_file (fname);
  free (fname);

  local_finish ();
  dot_plugin_common.finish ();

  return;
}

/* Public function to view call graph.  */

void
dot_plugin_view_cgraph (void)
{
  dot_plugin_common.init ();
  local_init ();

  dump_cgraph_to_file (dot_plugin_common.temp_file_name);
  dot_plugin_common.show (dot_plugin_common.temp_file_name);

  local_finish ();
  dot_plugin_common.finish ();

  return;
}

/* Plugin callback function for PLUGIN_ALL_IPA_PASSES_START event.  */

void *
dot_plugin_callback_cgraph (void *gcc_data, void *user_data)
{
    printf("%s():\n",__FUNCTION__);
  dot_plugin_dump_cgraph ();
  return NULL;
}

/* Plugin callback function for PLUGIN_ALL_IPA_PASSES_START event.  */

void *
dot_plugin_callback_callee (void *gcc_data, void *user_data)
{
    printf("%s():\n",__FUNCTION__);
  dot_plugin_dump_cgraph_callee ();
  return NULL;
}

/* Plugin callback function for PLUGIN_ALL_IPA_PASSES_START event.  */

void *
dot_plugin_callback_caller (void *gcc_data, void *user_data)
{
  printf("%s():\n",__FUNCTION__);
  dot_plugin_dump_cgraph_caller ();
  return NULL;
}

/* end */
